package com.sskj.linechat.widght;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.Nullable;

import com.sskj.linechat.bean.LineBean;
import com.sskj.linechat.utils.ScrollView;
import com.sskj.linechat.utils.UIUtil;

import java.util.HashMap;
import java.util.List;

public class LineView extends ScrollView {

    private HashMap<Integer, String> bgColorMap = new HashMap<>();
    //镜像
    private HashMap<Integer, String> bgColorMapMirror = new HashMap<>();

    private List<LineBean> listData;

    private float tvWidth, tvHeight, lineHeight, tvCycleWidth, layoutWidth;
    private Point[] points;
    private float scrollHeight;
    private float radiusX, radiusY;

    public LineView(Context context) {
        this(context, null);
    }

    public LineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public float getBottomSlide() {
        return scrollHeight;
    }


    private Paint paintLine, paintTv, paintText, paintCycle;

    private void initPaint() {
        //背景色
        bgColorMap.put(0, "#2BD798");//绿
        bgColorMap.put(1, "#000000");//黑
        bgColorMap.put(2, "#FF6D74");//红
        //镜像背景色
        bgColorMapMirror.put(0, "#332BD798");//绿
        bgColorMapMirror.put(1, "#000000");//黑
        bgColorMapMirror.put(2, "#33FF6D74");//红


//        bgColorMap.put(0, "#FF6D74");//红
//        bgColorMap.put(1, "#000000");//黑
//        bgColorMap.put(2, "#2BD798");//绿

        tvWidth = UIUtil.dp2px(50);
        tvHeight = UIUtil.dp2px(20);
        lineHeight = UIUtil.dp2px(20);
        tvCycleWidth = UIUtil.dp2px(50);
        radiusX = UIUtil.dp2px(6);
        radiusY = UIUtil.dp2px(6);

        paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintLine.setColor(Color.BLACK);
        paintLine.setStrokeWidth(5f);
        paintLine.setStyle(Paint.Style.STROKE);
        DashPathEffect pathEffect = new DashPathEffect(new float[]{20, 20}, 5);
        paintLine.setPathEffect(pathEffect);

        paintTv = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintTv.setStyle(Paint.Style.FILL);

        paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintText.setTextSize(UIUtil.dp2px(8));
        paintText.setColor(Color.WHITE);
        paintText.setStyle(Paint.Style.FILL);
        paintText.setTextAlign(Paint.Align.CENTER);

        paintCycle = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintCycle.setTextSize(UIUtil.dp2px(20));
        paintCycle.setColor(Color.parseColor("#333333"));
        paintCycle.setStyle(Paint.Style.FILL);
        paintCycle.setTextAlign(Paint.Align.CENTER);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        DrawCycleNum(canvas);
        DrawTextBg(canvas);
        DrawBrokenLine(canvas);

    }


    public void getPoints(List<LineBean> list, float layoutWidth, float layoutHeight) {
        this.listData = list;
        this.layoutWidth = layoutWidth;
        float viewHeight = (list.size() * tvHeight) + (lineHeight * (list.size() - 1));
        scrollHeight = viewHeight > layoutHeight ? viewHeight - layoutHeight : 0;
        Log.e("TAG", "tvHeight:" + tvHeight + "lineHeight:" + lineHeight);
        Log.e("TAG", "滑动高度：" + scrollHeight);
        Point[] points = new Point[list.size()];
        for (int i = 0; i < list.size(); i++) {
            float pointX = 0, pointY = 0;
            int type = list.get(i).getType();
            boolean holdIsLeft = list.get(i).isHoldIsLeft();
            if (type == 1) {
                type = holdIsLeft ? 0 : 2;
            }
            if (type == 0) {
                pointX = 0;
                pointY = lineHeight * i + tvHeight * i;
            } else if (type == 2) {
                pointX = layoutWidth - tvWidth;
                pointY = lineHeight * i + tvHeight * i;
            }
            Point point = new Point((int) pointX, (int) pointY);
            points[i] = point;
        }
        this.points = points;
        invalidate();
    }

    /**
     * 根据值绘制折线
     */
    private void DrawBrokenLine(Canvas canvas) {
        if (points == null || points.length == 0) {
            return;
        }
        Path mPath = new Path();
        for (int i = 0; i < points.length; i++) {
            if (i == points.length - 1) {
                continue;
            }
            Point point = points[i];
            Point point1 = points[i + 1];

            if (point.x == point1.x) {
                mPath.moveTo(point.x + tvWidth / 2 + tvCycleWidth, point.y + tvHeight);
                mPath.lineTo(point1.x + tvWidth / 2 + tvCycleWidth, point1.y);
            } else if (point.x > point1.x) {
                mPath.moveTo(point.x + tvCycleWidth, point.y + tvHeight);
                mPath.lineTo(point1.x + tvWidth + tvCycleWidth, point1.y);
            } else {
                mPath.moveTo(point.x + tvWidth + tvCycleWidth, point.y + tvHeight);
                mPath.lineTo(point1.x + tvCycleWidth, point1.y);
            }
        }
        canvas.drawPath(mPath, paintLine);
    }

    /**
     * 绘制线上的方块
     */
    private void DrawTextBg(Canvas canvas) {
        if (points == null || points.length == 0) {
            return;
        }
        for (int i = 0; i < points.length; i++) {
            Point point = points[i];
            paintTv.setColor(Color.parseColor(bgColorMap.get(listData.get(i).getType())));
            //如果是hold，则是黑边框黑字，buy是绿底白字，sell是红底白字
            if (listData.get(i).getType() == 1) {
                paintText.setColor(Color.BLACK);
                paintTv.setStyle(Paint.Style.STROKE);
            } else {
                paintText.setColor(Color.WHITE);
                paintTv.setStyle(Paint.Style.FILL);
            }
            //定义矩形
            Rect rect = new Rect(point.x, point.y, point.x + (int) tvWidth, point.y + (int) tvHeight);
            //绘制矩形
//            canvas.drawRect(point.x+ tvCycleWidth, point.y, point.x + tvWidth+ tvCycleWidth, point.y + tvHeight, paintTv);
            //绘制原角矩形
            canvas.drawRoundRect(point.x + tvCycleWidth, point.y, point.x + tvWidth + tvCycleWidth, point.y + tvHeight, radiusX, radiusY, paintTv);
            //绘制镜像
            paintTv.setColor(Color.parseColor(bgColorMapMirror.get(listData.get(i).getType())));
            if (point.x == 0) {
                canvas.drawRoundRect(layoutWidth - tvWidth+tvCycleWidth, point.y, layoutWidth+tvCycleWidth, point.y + tvHeight, radiusX, radiusY, paintTv);
            } else {
                canvas.drawRoundRect(tvCycleWidth, point.y, tvCycleWidth + tvWidth, point.y + tvHeight, radiusX, radiusY, paintTv);
            }

            String tipText = listData.get(i).getPrice();
            //计算baseline
            Paint.FontMetrics fontMetrics = paintText.getFontMetrics();
            float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
            float baseline = rect.centerY() + distance;
//            Log.e("TAG", "fontMetrics.bottom:" + fontMetrics.bottom + "fontMetrics.top:" + fontMetrics.top + "rect.centerY():" + rect.centerY());
            canvas.drawText(tipText, rect.centerX() + tvCycleWidth, baseline, paintText);
        }
    }


    /**
     * 绘制期数
     */
    private void DrawCycleNum(Canvas canvas) {
        if (points == null || points.length == 0) {
            return;
        }
        for (int i = 0; i < points.length; i++) {
            Point point = points[i];
            Rect rect = new Rect(0, point.y, (int) tvWidth, point.y + (int) tvHeight);

            String tipText = listData.get(i).getCycleNum();
            //计算baseline
            Paint.FontMetrics fontMetrics = paintCycle.getFontMetrics();
            float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
            float baseline = rect.centerY() + distance;
//            Log.e("TAG", "fontMetrics.bottom:" + fontMetrics.bottom + "fontMetrics.top:" + fontMetrics.top + "rect.centerY():" + rect.centerY());
            canvas.drawText(tipText, rect.centerX(), baseline, paintCycle);
        }
    }
}
